<?php

namespace Spider\Weibo\Api;

use Spider\Weibo\Config\Config;
use Spider\Weibo\Utils\HttpClient;

class PostApi
{
    private $http;
    private $maxRetries;
    private $retryDelay;

    public function __construct()
    {
        $config = Config::getInstance();
        $requestConfig = $config->getRequest();

        $this->maxRetries = $requestConfig['max_retries'];
        $this->retryDelay = $requestConfig['retry_delay'];
        $this->http = new HttpClient();
    }

    private function handleResponse(array $response, int $retries = 0): array
    {
        if (isset($response['error_code']) && $response['error_code'] == 401) {
            if ($retries < $this->maxRetries) {
                sleep($this->retryDelay);
                return $this->retryRequest('last', $retries + 1);
            }
            throw new \LogicException("Request failed with error 401 after {$this->maxRetries} retries");
        }

        if (isset($response['ok']) && $response['ok'] == -100) {
            if ($retries < $this->maxRetries) {
                sleep($this->retryDelay);
                return $this->retryRequest('last', $retries + 1);
            }
            throw new \LogicException("Login required after {$this->maxRetries} retries");
        }

        return $response;
    }

    private function retryRequest(string $type, int $retries): array
    {
        switch ($type) {
            case 'posts':
                return $this->getUserPosts($this->lastUid, $this->lastPage, $retries);
            case 'detail':
                return $this->getPostDetail($this->lastId, $retries);
            case 'longtext':
                return $this->getPostLongText($this->lastId, $retries);
            default:
                throw new \LogicException("Unknown request type");
        }
    }

    private $lastUid;
    private $lastPage;
    private $lastId;
    private $lastRequestType;

    public function getUserPosts(string $uid, int $page = 1, int $retries = 0): array
    {
        $this->lastUid = $uid;
        $this->lastPage = $page;
        $this->lastRequestType = 'posts';

        $response = $this->http->get('https://weibo.com/ajax/statuses/mymblog', [
            'uid' => $uid,
            'page' => $page,
            'feature' => 0
        ]);
        return $this->handleResponse($response, $retries);
    }

    public function getPostDetail(string $id, int $retries = 0): array
    {
        $this->lastId = $id;
        $this->lastRequestType = 'detail';

        $response = $this->http->get("https://weibo.com/ajax/statuses/show", [
            'id' => $id
        ]);
        return $this->handleResponse($response, $retries);
    }

    public function getPostLongtext(string $id, int $retries = 0): array
    {
        $this->lastId = $id;
        $this->lastRequestType = 'longtext';

        $response = $this->http->get("https://weibo.com/ajax/statuses/longtext", [
            'id' => $id
        ]);
        return $this->handleResponse($response, $retries);
    }
}
